{% extends "base.html" %}
{% block content %}
<h2>{% if patient %}Edit Patient{% else %}Add New Patient{% endif %}</h2>

{% if error %}
<p style="color:red">{{ error }}</p>
{% endif %}

<!-- Patient Form -->
<form method="post">
  Name: <input name="name" value="{{ patient.name if patient else '' }}" required><br>
  Gender:
  <select name="gender">
    <option value="男" {% if patient and patient.gender=='男' %}selected{% endif %}>Male</option>
    <option value="女" {% if patient and patient.gender=='女' %}selected{% endif %}>Female</option>
  </select><br>
  Age: <input name="age" type="number" min="1" value="{{ patient.age if patient else '' }}" required><br>
  Phone: <input name="phone" value="{{ patient.phone if patient else '' }}" required><br>
  Email: <input name="email" value="{{ patient.email if patient else '' }}"><br>
  Address: <input name="address" value="{{ patient.address if patient else '' }}"><br>
  Medical History: <textarea name="medical_history">{{ patient.medical_history if patient else '' }}</textarea><br>
  <button type="submit">Save</button>
</form>

<hr>

<!-- Patients List -->
<h2>Patients List</h2>
{% if patients %}
<table border="1" cellpadding="5" cellspacing="0">
  <tr>
    <th>ID</th>
    <th>Name</th>
    <th>Gender</th>
    <th>Age</th>
    <th>Phone</th>
    <th>Email</th>
    <th>Actions</th>
  </tr>
  {% for p in patients %}
  <tr>
    <td>{{ p.patient_id }}</td>
    <td>{{ p.name }}</td>
    <td>{{ p.gender }}</td>
    <td>{{ p.age }}</td>
    <td>{{ p.phone }}</td>
    <td>{{ p.email }}</td>
    <td>
      <a href="{{ url_for('patient_edit', patient_id=p.patient_id) }}">Edit</a>
      <form action="{{ url_for('patient_delete', patient_id=p.patient_id) }}" method="post" style="display:inline;">
        <button type="submit" onclick="return confirm('Are you sure you want to delete this patient?')">Delete</button>
      </form>
    </td>
  </tr>
  {% endfor %}
</table>
{% else %}
<p>No patients found.</p>
{% endif %}

{% endblock %}
